#include "cloud_config.hpp"
#include "hotspot.hpp"
#include "service.hpp"
#include <thread>

cloud::DataManager* _data = new cloud::DataManager();

void TestFileUtil(const std::string file_name)
{
  /*
  cloud::FileUtil util(file_name);
  std::cout << util.FileSize() << std::endl;
  std::cout << util.LastMTime() << std::endl;
  std::cout << util.LastATime() << std::endl;
  std::cout << util.FileName() << std::endl;
  */
  
  /*
  cloud::FileUtil read_file(file_name);
  std::string body;
  read_file.GetFileContent(body);
  
  cloud::FileUtil write_file("./test_write.txt");
  write_file.SetFileContent(body);
  */
  
  /*
  std::string pathname = file_name + ".lz";
  cloud::FileUtil compress_file(file_name);
  compress_file.Compress(pathname);

  cloud::FileUtil uncompress_file(pathname);
  uncompress_file.UnCompress("./uncompress_file_test.txt");
  */

  cloud::FileUtil fu(file_name);
  fu.CreateDirectory();
  std::vector<std::string> arr;
  fu.ScanDirectory(arr);
  for(const auto& e : arr)
  {
    std::cout << e << std::endl;
  }
}

void JsonUtilTest()
{
  std::string json_str;
  const char* name = "json";
  int age = 18;
  float score[] = {95, 96, 99.9};
  Json::Value root;
  root["name"] = name;
  root["age"] = age;
  root["score"].append(score[0]);
  root["score"].append(score[1]);
  root["score"].append(score[2]);
  cloud::JsonUtil::Serialize(root, json_str);
  std::cout << json_str << std::endl;

  Json::Value val;
  cloud::JsonUtil::UnSerialize(json_str, val);
  std::cout << val["name"].asString() << std::endl;
  std::cout << val["age"].asInt() << std::endl;
  for(int i = 0; i < val["score"].size(); ++i)
  {
    std::cout << val["score"][i].asFloat() << std::endl;
  }
}

void ConfigTest()
{
  cloud::Config* conf = cloud::Config::_get_instance();
  std::cout << conf->GetHotTime() << std::endl;
  std::cout << conf->GetServerPort() << std::endl;
  std::cout << conf->GetServerIp() << std::endl;
  std::cout << conf->GetURLPrefix() << std::endl;
  std::cout << conf->GetBackDir() << std::endl;
  std::cout << conf->GetPackDir() << std::endl;
  std::cout << conf->GetManagerFile() << std::endl;
  std::cout << conf->GetArcSuffix() << std::endl;
}

void DataTest(const std::string& file_name)
{
  /*
  cloud::BackupInfo backup_info;
  backup_info.InitBackupInfo(file_name);
  */

  cloud::DataManager dm;
  
  /*
  dm.Insert(backup_info);
  cloud::BackupInfo tmp;
  dm.GetOneByURL("/download/bundle.h", tmp);
  std::cout << "----------Insert and GetOneByURL----------" << std::endl;
  std::cout << tmp.pack_flag << std::endl;
  std::cout << tmp.mtime << std::endl;
  std::cout << tmp.atime << std::endl;
  std::cout << tmp.fsize << std::endl;
  std::cout << tmp.real_path << std::endl;
  std::cout << tmp.url << std::endl;
  std::cout << tmp.pack_path << std::endl;

  tmp.pack_flag = true;
  dm.Updata(tmp);
  */

  std::vector<cloud::BackupInfo> arr;
  dm.GetAll(arr);

  //std::cout << "----------Updata and GetAll----------" << std::endl;
  
  for(auto& e : arr)
  {
    std::cout << e.pack_flag << std::endl;
    std::cout << e.mtime << std::endl;
    std::cout << e.atime << std::endl;
    std::cout << e.fsize << std::endl;
    std::cout << e.real_path << std::endl;
    std::cout << e.url << std::endl;
    std::cout << e.pack_path << std::endl;
  }
  
  /*
  cloud::Config* conf = cloud::Config::_get_instance();
  dm.GetOneByRealPath(conf->GetBackDir() + file_name, tmp);
  std::cout << "----------GetOneByRealPath----------" << std::endl;
  std::cout << tmp.pack_flag << std::endl;
  std::cout << tmp.mtime << std::endl;
  std::cout << tmp.atime << std::endl;
  std::cout << tmp.fsize << std::endl;
  std::cout << tmp.real_path << std::endl;
  std::cout << tmp.url << std::endl;
  std::cout << tmp.pack_path << std::endl;
  */
}

void HotTimeTest()
{
  cloud::HotManager hm;
  hm.RunModule();
}

void ServiceTest()
{
  cloud::Service ser;
  ser.RunModule();
}

int main(int agrc, char* argv[])
{
  //TestFileUtil(argv[1]);
  //JsonUtilTest();
  //ConfigTest();
  //DataTest(argv[1]);
  //HotTimeTest();
  //ServiceTest();
  std::thread thread_hot_manager(HotTimeTest);
  std::thread thread_service(ServiceTest);
  thread_hot_manager.join();
  thread_service.join();
  return 0;
}
